<?php
class ThanhtichkhoasvsController extends AppController {

	var $name = 'Thanhtichkhoasvs';
    var $uses= array('Thanhtichkhoasv','Hockie','Tgiachidoan','Chidoansv');
    var $components = array('RequestHandler'); 
   
	function index($phongtrao_id)
	{
		$this->Thanhtichkhoasv->recursive = 0;
		$this->set('thanhtichkhoasvs', $this->paginate(array('phongtraokhoa_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichkhoasv->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
		
		$this->set(compact('phongtrao','phongtrao_id'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thanhtichkhoasv', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thanhtichkhoasv', $this->Thanhtichkhoasv->read(null, $id));
	}

	function viewtt($mssv){
        if($mssv==null){
            $this->Session->setFlash(__('Mã số sinh viên không hợp lệ', true));
        }else{
            $hocky = $this->getHocKyHt();
    		$x = $hocky['Hocky']['ngaybatdau'];
    		$y = $hocky['Hocky']['ngayketthuc'];
            $thanhtichkhoasvs=$this->Thanhtichkhoasv->find('all',array('conditions'=>array('sinhvien_id'=>$mssv, array('Phongtraokhoa.ngayketthuc BETWEEN ? AND ?' => array($x,$y)))));
            //pr($thanhtichs);
            $this->set(compact('thanhtichkhoasvs'));
        }
    }

	function add($phongtrao_id)
	{
		if (!empty($this->data))
		{
			$this->Thanhtichkhoasv->create();
			
			$this->data['Thanhtichkhoasv']['phongtraokhoa_id'] = $phongtrao_id;
			if(!isset($this->data['Thanhtichkhoasv']['sinhvien_id']))
				$this->data['Thanhtichkhoasv']['sinhvien_id'] = $this->data['sinhvien_id'];
			if ($this->Thanhtichkhoasv->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'add',$phongtrao_id));
			}
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		$this->Thanhtichkhoasv->recursive = 0;
		$this->set('thanhtichkhoasvs', $this->paginate(array('phongtraokhoa_id'=>$phongtrao_id)));
		$phongtrao = $this->Thanhtichkhoasv->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
    	$khoa_id = $phongtrao['Phongtraokhoa']['khoa_id'];
    	$querystring= "select Tgiachidoan.chidoansv_id
    					from tgiachidoans as Tgiachidoan, (select a.chidoansv_id, count(sinhvien_id) as sl from tgiachidoans a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoan1
    					where Tgiachidoan.chidoansv_id = Tgiachidoan1.chidoansv_id and Tgiachidoan1.sl>0 and ngaychuyen is NULL
    						group by Tgiachidoan.chidoansv_id";
    	$laycd_act = $this->Thanhtichkhoasv->query($querystring);
    	$cd_id = array();
    	foreach ($laycd_act as $cd) 
    	{
    		$cd_id[] = $cd['Tgiachidoan']['chidoansv_id'];
    	}
    	//$this->Chidoansv->displayField='chidoan' ;
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id !='=>null,'id'=>$cd_id),'fields'=>array('chidoan')));
    	$chidoan_id =array_keys($chidoans);
    	$svdaco = $this->Thanhtichkhoasv->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id),'fields'=>array('sinhvien_id')));
    	$svs = $this->Tgiachidoan->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.mssv','Sinhvien.hoten')));//,'NOT'=>array('Sinhvien.id'=>$svdaco)
    	foreach ($svs as $sv)
    	{
    		$sinhviens[$sv['Sinhvien']['id']]=$sv['Sinhvien']['mssv'].'-'.$sv['Sinhvien']['hoten'];
    	}
		$this->set(compact('sinhviens', 'phongtrao','phongtrao_id','chidoans'));
	}

	function edit($id = null,$phongtrao_id) {
		if (!empty($this->data))
		{
			$this->Thanhtichkhoasv->create();
			
			$this->data['Thanhtichkhoasv']['phongtraokhoa_id'] = $phongtrao_id;
			if(!isset($this->data['Thanhtichkhoasv']['sinhvien_id']))
				$this->data['Thanhtichkhoasv']['sinhvien_id'] = $this->data['sinhvien_id'];
			if ($this->Thanhtichkhoasv->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$phongtrao_id));
			}
			else
			{
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true),'error');
			}
		}
		
		if (empty($this->data))
		{
			$this->data = $this->Thanhtichkhoasv->read(null, $id);
		}
		$sinhvien_id = $this->data['Thanhtichkhoasv']['sinhvien_id'];
		$phongtrao = $this->Thanhtichkhoasv->Phongtraokhoa->find('first',array('recursive'=>-1,'conditions'=>array('Phongtraokhoa.id'=>$phongtrao_id)));
    	$khoa_id = $phongtrao['Phongtraokhoa']['khoa_id'];
    	$chidoans = $this->Chidoansv->find('list',array('conditions'=>array('khoa_id'=>$khoa_id,'khoahoc_id !='=>null),'fields'=>array('chidoan')));
    	$chidoan_id =array_keys($chidoans);
    	$svdaco = $this->Thanhtichkhoasv->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id,'NOT'=>array('sinhvien_id'=>$sinhvien_id)),'fields'=>array('sinhvien_id')));
		$svs = $this->Tgiachidoan->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id),'fields'=>array('Sinhvien.id','Sinhvien.mssv','Sinhvien.hoten')));//,'NOT'=>array('Sinhvien.id'=>$svdaco)
    	foreach ($svs as $sv)
    	{
    		$sinhviens[$sv['Sinhvien']['id']]=$sv['Sinhvien']['mssv'].'-'.$sv['Sinhvien']['hoten'];
    	}
		$this->set(compact('sinhviens', 'phongtrao','phongtrao_id','chidoans'));
	}

	function delete($id = null,$phongtrao_id) {
		if (!$id) {
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		if ($this->Thanhtichkhoasv->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$phongtrao_id));
		}
		$this->Session->setFlash(__('Không thể xóa vui lòng thử lại', true),'error');
		$this->redirect(array('action' => 'index',$phongtrao_id));
	}

	function laysinhvien($phongtrao_id)
	{
		$this->layout = 'ajax';
		$chidoan_id = $this->data['Thanhtichkhoasv']['chidoan_id'];
		
		$svdaco = $this->Thanhtichkhoasv->find('list',array('conditions'=>array('phongtraokhoa_id'=>$phongtrao_id),'fields'=>array('sinhvien_id')));
    	$svs = $this->Tgiachidoan->find('all',array('recursive'=>'0','conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,'NOT'=>array('Sinhvien.id'=>$svdaco)),'fields'=>array('Sinhvien.id','Sinhvien.mssv','Sinhvien.hoten')));
		foreach ($svs as $sv)
    	{
    		$sinhviens[$sv['Sinhvien']['id']]=$sv['Sinhvien']['mssv'].'-'.$sv['Sinhvien']['hoten'];
    	}
		$this->set(compact('sinhviens'));
	}

}
